package util;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import util.mail.MailInfo;
import util.mail.MailSendUtil;

public class SendMail {

	private String host;
	private String formName;
	private String formPassword;
	private String replayAddress;
	private String toAddress;
	private String subject;
	private String content;
	private String contentImage;
	private String[] fileUrl;
	private String contentImageID;
	
//	public static void sendMail(){
////		 Transport.send();
//		 new SendMail(host);
//		
//	}

	public String getContentImageID() {
		return contentImageID;
	}

	public void setContentImageID(String contentImageID) {
		this.contentImageID = contentImageID;
	}

	public String getContentImage() {
		return contentImage;
	}

	public void setContentImage(String contentImage) {
		this.contentImage = contentImage;
	}

	public String[] getFileUrl() {
		return fileUrl;
	}

	public void setFileUrl(String[] fileUrl) {
		this.fileUrl = fileUrl;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getFormPassword() {
		return formPassword;
	}

	public void setFormPassword(String formPassword) {
		this.formPassword = formPassword;
	}

	public String getReplayAddress() {
		return replayAddress;
	}

	public void setReplayAddress(String replayAddress) {
		this.replayAddress = replayAddress;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * 创建内含附件、图文并茂的邮件
	 * 
	 * @author haolloyin
	 */

	/**
	 * 根据传入的文件路径创建附件并返回
	 */
	public MimeBodyPart createAttachment(String fileName) throws Exception {
		MimeBodyPart attachmentPart = new MimeBodyPart();
		FileDataSource fds = new FileDataSource(fileName);
		attachmentPart.setDataHandler(new DataHandler(fds));
		attachmentPart.setFileName(fds.getName());
		return attachmentPart;
	}

	/**
	 * 根据传入的邮件正文body和文件路径创建图文并茂的正文部分
	 */
	public MimeBodyPart createContent(SendMail info) throws Exception {
		// 用于保存最终正文部分
		MimeBodyPart contentBody = new MimeBodyPart();
		// 用于组合文本和图片，"related"型的MimeMultipart对象
		MimeMultipart contentMulti = new MimeMultipart("related");

		// 正文的文本部分
		MimeBodyPart textBody = new MimeBodyPart();
		textBody.setContent(info.getContent(), "text/html;charset=utf-8");
		contentMulti.addBodyPart(textBody);

		// 正文的图片部分
		MimeBodyPart jpgBody = new MimeBodyPart();
		FileDataSource fds = new FileDataSource(info.getContentImage());
		jpgBody.setDataHandler(new DataHandler(fds));
		// jpgBody.setFileName(fds.getName());没有设置正文图片名字则不会在正文中显示该图片并且会变成附件中后缀名为.bin的该正文图片文件
		jpgBody.setFileName(fds.getName());
		jpgBody.setContentID(info.getContentImageID());
		contentMulti.addBodyPart(jpgBody);

		// 将上面"related"型的 MimeMultipart 对象作为邮件的正文
		contentBody.setContent(contentMulti);
		return contentBody;
	}

	/**
	 * 根据传入的 Seesion 对象创建混合型的 MIME消息
	 */
	// public MimeMessage createMessage(Session session) throws Exception {
	// String from = "17876571503@163.com";
	// String to = "test_hao@sina.cn";
	// String subject = "创建内含附件、图文并茂的邮件！";
	// String body = "<h4>内含附件、图文并茂的邮件测试！！！</h4> </br>"
	// + "<a href = http://haolloyin.blog.51cto.com/> 蚂蚁</a></br>"
	// + "<img src = \"cid:logo_jpg\"></a>";
	//
	// MimeMessage msg = new MimeMessage(session);
	// msg.setFrom(new InternetAddress(from));
	// msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
	// msg.setSubject(subject);
	//
	// // 创建邮件的各个 MimeBodyPart 部分
	// MimeBodyPart attachment01 = createAttachment("F:\\java\\Snake.java");
	// MimeBodyPart attachment02 = createAttachment("F:\\java\\meng.mp3");
	// MimeBodyPart content = createContent(body, "F:\\java\\logo.jpg");
	//
	// // 将邮件中各个部分组合到一个"mixed"型的 MimeMultipart 对象
	// MimeMultipart allPart = new MimeMultipart("mixed");
	// allPart.addBodyPart(attachment01);
	// allPart.addBodyPart(attachment02);
	// allPart.addBodyPart(content);
	//
	// // 将上面混合型的 MimeMultipart 对象作为邮件内容并保存
	// msg.setContent(allPart);
	// msg.saveChanges();
	// return msg;
	// }

	private Message getMessage(SendMail info) throws Exception {
		final Properties p = System.getProperties();
		p.setProperty("mail.smtp.host", info.getHost());
		p.setProperty("mail.smtp.auth", "true");
		p.setProperty("mail.smtp.user", info.getFormName());
		p.setProperty("mail.smtp.pass", info.getFormPassword());
		// new Authenticator()应该是验证密码如果没有加的话会报
		// failed to connect, no password specified?错误
		Session session = Session.getInstance(p, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(p
						.getProperty("mail.smtp.user"), p
						.getProperty("mail.smtp.pass"));
			}
		});
		session.setDebug(true);
		Message message = new MimeMessage(session);
		message.setSubject(info.getSubject());
		message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
		message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),
				"WSF"));
		message.setRecipient(RecipientType.TO,
				new InternetAddress(info.getToAddress()));
		// 创建邮件的各个 MimeBodyPart 部分
		// 将邮件中各个部分组合到一个"mixed"型的 MimeMultipart 对象
		MimeMultipart allPart = new MimeMultipart("mixed");
		if (info.getFileUrl() != null && info.getFileUrl().length > 0) {
			for (String fileUrl : info.getFileUrl())
				allPart.addBodyPart(createAttachment(fileUrl));
		}

		MimeBodyPart content = createContent(info);
		allPart.addBodyPart(content);

		// 将上面混合型的 MimeMultipart 对象作为邮件内容并保存
		message.setContent(allPart);
		message.saveChanges();
		return message;
	}

	// 测试生成邮件
	public static void main(String[] args) throws Exception {
		// SendMail mail = new SendMail();
		// Session session = Session.getDefaultInstance(new Properties());
		// MimeMessage message = mail.createMessage(session);
		// message.writeTo(new FileOutputStream("withAttachmentMail.eml"));
		
		
//		SendMail info = new SendMail();
//		info.setHost("smtp.163.com");
//		// kundianhuo@sina.com
//		// 17876571503@163.com
//		info.setFormName("17876571503@163.com");
//		info.setFormPassword("wap5259177");
//		// kundianhuo@sina.com
//		info.setReplayAddress("17876571503@163.com");
//		info.setToAddress("1426256791@qq.com");
//		info.setSubject("testImageAndTextEmail");
//		info.setContentImage("C:\\Users\\WSF\\Desktop\\timg.jpg");
//		info.setContentImageID("a");
//		info.setContent("<a href = http://haolloyin.blog.51cto.com/>张三辞职信<input type=\"image\" src=\"cid:"
//				+ info.getContentImageID()
//				+ "\"></a></br><div title='666'>test</div>");
		String[] fileUrl = new String[2];
		fileUrl[0] = "C:\\Users\\WSF\\Desktop\\timg.jpg";
		fileUrl[1] = "C:\\Users\\WSF\\Desktop\\宽带.txt";
//		info.setFileUrl(fileUrl);
//
//		
//		MailInfo mail=MailInfo.MailInfoFactory("smtp.163.com", "17876571503@163.com", "wap5259177", "17876571503@163.com", "1426256791@qq.com", "testImageAndTextEmail", "<a href = http://haolloyin.blog.51cto.com/>张三辞职信<input type=\"image\" src=\"cid:"
//				+"a"
//				+ "\"></a></br><div title='666'>test</div>", "C:\\Users\\WSF\\Desktop\\timg.jpg", fileUrl, "a");
//		
//		Transport.send(info.getMessage(info));
		
	
//		MailSendUtil.sendMail(MailInfo.MailInfoFactory("smtp.163.com", "17876571503@163.com", "wap5259177", "17876571503@163.com", "1426256791@qq.com", "testImageAndTextEmail", "<a href = http://haolloyin.blog.51cto.com/>张三辞职信<input type=\"image\" src=\"cid:"
//				+"a"
//				+ "\"></a></br><div title='666'>test</div>", "C:\\Users\\WSF\\Desktop\\timg.jpg", fileUrl, "a"));
		
		List<MailInfo> infos=new ArrayList<>();
		infos.add(MailInfo.MailInfoFactory("smtp.163.com", "17876571503@163.com", "wap5259177", "17876571503@163.com", "1426256791@qq.com", "testImageAndTextEmail", "<a href = http://haolloyin.blog.51cto.com/>张三辞职信<input type=\"image\" src=\"cid:"
				+"a"
				+ "\"></a></br><div title='666'>测试0</div>", "C:\\Users\\WSF\\Desktop\\timg.jpg", fileUrl, "a"));
		infos.add(MailInfo.MailInfoFactory("smtp.163.com", "17876571503@163.com", "wap5259177", "17876571503@163.com", "1426256791@qq.com", "testImageAndTextEmail", "<a href = http://haolloyin.blog.51cto.com/>张三辞职信<input type=\"image\" src=\"cid:"
				+"a"
				+ "\"></a></br><div title='666'>测试1</div>", "C:\\Users\\WSF\\Desktop\\timg.jpg", fileUrl, "a"));
		infos.add(MailInfo.MailInfoFactory("smtp.163.com", "17876571503@163.com", "wap5259177", "17876571503@163.com", "1426256791@qq.com", "testImageAndTextEmail", "<a href = http://haolloyin.blog.51cto.com/>张三辞职信<input type=\"image\" src=\"cid:"
				+"a"
				+ "\"></a></br><div title='666'>测试2</div>", "C:\\Users\\WSF\\Desktop\\timg.jpg", fileUrl, "a"));
		MailSendUtil.sendMail(infos);
		//
		// String filePath1 = "C:\\Users\\Administrator\\Desktop\\wsfjpg.jpg";
		// String filePath2 = "C:\\Users\\Administrator\\Desktop\\abc.swf";
		// String filePath3 =
		// "C:\\Users\\Administrator\\Desktop\\javaee重点备忘.docx";
	}
}
